package com.smt.ejb3.tutoria.service.impl;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.smt.ejb3.tutoria.entitybean.Person;
import com.smt.ejb3.tutoria.service.PersonService;

@Stateless
@Remote(PersonService.class)
public class PersonServiceImpl implements PersonService {

	@PersistenceContext(unitName="ejb3db")
	private EntityManager em;
	
	@Override
	public void save(Person person) {
		em.persist(person);
	}

	@Override
	public void update(Person person) {
		em.merge(person);
	}

	@Override
	public void delete(Integer personId) {
		//getReference returns a proxy.
		//It doesn't load the data immediately
		em.remove(em.getReference(Person.class, personId));
	}

	@Override
	public Person getPerson(Integer personId) {
		return em.find(Person.class, personId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Person> getPersons() {
		return em.createQuery("select p from Person p").getResultList();
	}

}
